/* GUI CONVERTED from reupdcac.p (converter v1.76) Tue Apr 23 11:44:00 2002 */
/* reupdcac.p - REPETITIVE                                                   */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                       */
/* All rights reserved worldwide.  This is an unpublished work.              */
/* $Revision: 1.16 $                                                         */
/*V8:ConvertMode=Maintenance                                                 */
/* REVISION: 7.3      LAST MODIFIED: 11/08/94   BY: WUG *GO42*               */
/* REVISION: 7.3      LAST MODIFIED: 09/27/95   BY: STR *G0Y1*               */
/* REVISION: 7.3      LAST MODIFIED: 11/22/95   BY: JYM *G1DL*               */
/* REVISION: 7.3      LAST MODIFIED: 08/06/97   BY: *G2P0* Manmohan Pardesi  */
/* REVISION: 9.0      LAST MODIFIED: 04/16/99   BY: *J2DG* Reetu Kapoor      */
/* REVISION: 9.1      LAST MODIFIED: 11/17/99   BY: *N04H* Vivek Gogte       */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* myb               */
/* REVISION: 9.1      LAST MODIFIED: 12/01/00   BY: *L0P5* Sathish Kumar     */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* $Revision: 1.16 $    BY: Robin McCarthy        DATE: 01/31/02  ECO: *P000*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* UPDATE CURRENT AVERAGE/LAST COST                                           */
/* THE FOLLOWING IS THE ACCOUNT OF DESIGNATIONS AS USED IN ADVANCED REPETETIVE*/
/* COSTING. 'tl' REFERS TO THE CURRENT OPERATION and 'll' REFERS TO PREVIOUS  */
/* OPERATIONS. (THESE DESIGNATIONS DO NOT CORRELATE TO this-level AND lower-  */
/* level ITEM COSTS.)                                                         */
/* wr_xxx_act     - The actual labor, burden, and subcontract costs reported  */
/*                  for the quantity processed at this operation. The actual  */
/*                  material cost however is stored in the wr_mtl_ll_act      */
/*                  (G1GD) of the qad_wkfl.                                   */
/* wr_xxx_ll_act  - The accumulated labor, burden, and subcontract costs from */
/*                  the previous operations for the quantity processed at this*/
/*                  operation. The wr_mtl_ll_act however contains the         */
/*                  accumulated material costs for the quantity completed in  */
/*                  this operation.                                           */
/* wr_xxx_totx    - The total material, labor, burden, and subcontract cost   */
/*                  removed from WIP at this operation for the quantity       */
/*                  processed in the next operation                           */
/* wr_xxx_ll_totx - The accumulated total material, labor, burden, and        */
/*                  and subcontract cost removed from WIP from the previous   */
/*                  operations for the quantity completed in the next         */
/*                  operation. These are stored in the qad_wkfl.              */
/* unit_xxx_tl    - The local variables that contains the per unit material,  */
/*                  labor, burden, subcontract and overhead cost at the last  */
/*                  operation. These are used to update the items G/L Average */
/*                  cost and the Current Average or Last cost.                */
/* unit_xxx_ll    - The local variables that contains the per unit material,  */
/*                  labor, burden, subcontract and overhead cost at the last  */
/*                  operation from previous operations. These are used to     */
/*                  update the items G/L Average cost and the Current Average */
/*                  or Last cost.                                             */

/* IF YOU MAKE CHANGES TO THIS PROGRAM YOU MAY NEED TO CHANGE reupdgac.p ALSO */

{mfdeclre.i}

define input parameter wolot as character no-undo.
define input parameter qty_chg as decimal no-undo.

define variable glx_mthd like cs_method no-undo.
define variable glx_set  like cs_set    no-undo.
define variable cur_mthd like cs_method no-undo.
define variable cur_set  like cs_set    no-undo.

define variable wip_qty            as decimal no-undo.
define variable lbr_ll_wipval_xfer as decimal no-undo.
define variable bdn_ll_wipval_xfer as decimal no-undo.
define variable sub_ll_wipval_xfer as decimal no-undo.
define variable mtl_ll_wipval_xfer as decimal no-undo.
define variable lbr_tl_wipval_xfer as decimal no-undo.
define variable bdn_tl_wipval_xfer as decimal no-undo.
define variable sub_tl_wipval_xfer as decimal no-undo.
define variable mtl_tl_wipval_xfer as decimal no-undo.
define variable unit_lbr_ll        as decimal no-undo.
define variable unit_bdn_ll        as decimal no-undo.
define variable unit_sub_ll        as decimal no-undo.
define variable unit_mtl_ll        as decimal no-undo.
define variable unit_ovh_ll    as decimal no-undo.
define variable unit_lbr_tl        as decimal no-undo.
define variable unit_bdn_tl        as decimal no-undo.
define variable unit_sub_tl        as decimal no-undo.
define variable unit_mtl_tl        as decimal no-undo.
define variable unit_ovh_tl    as decimal no-undo.
define variable newlbr_ll          as decimal no-undo.
define variable newbdn_ll          as decimal no-undo.
define variable newsub_ll          as decimal no-undo.
define variable newmtl_ll          as decimal no-undo.
define variable newovh_ll      as decimal no-undo.
define variable newlbr_tl          as decimal no-undo.
define variable newbdn_tl          as decimal no-undo.
define variable newsub_tl          as decimal no-undo.
define variable newmtl_tl          as decimal no-undo.
define variable newovh_tl      as decimal no-undo.
define variable qty_oh             as decimal no-undo.
define variable new_qty_oh         as decimal no-undo.
define variable newcst         as decimal no-undo.
define variable reavg_yn like mfc_logical no-undo.
define variable curflag  like mfc_logical no-undo.
define variable glxflag  like mfc_logical no-undo.
define variable use_last like mfc_logical no-undo.

assign curflag = no
   glxflag = no
   use_last = no.

if qty_chg = 0 then leave.

for first wo_mstr
   fields (wo_lot wo_part wo_site)
   where wo_lot = wolot no-lock:
end. /* FOR FIRST WO_MSTR */

for first in_mstr
   fields (in_part in_qty_nonet in_qty_oh in_site
           in_gl_cost_site in_gl_set)
   where in_site = wo_site
   and in_part = wo_part no-lock:
end. /* FOR FIRST IN_MSTR */

if available in_mstr then
assign
   qty_oh = in_qty_oh + in_qty_nonet
   new_qty_oh = qty_oh + qty_chg.

/*DETERMINE COSTING METHOD*/
{gprun.i ""csavg01.p"" "(input wo_part, input wo_site,
     output glx_set, output glx_mthd, output cur_set, output cur_mthd)" }
/*GUI*/ if global-beam-me-up then undo, leave.


/*GET CURRENT COST SET*/
{gpsct01.i &set=cur_set &part=wo_part &site=wo_site}

find last wr_route where wr_lot = wo_lot exclusive-lock.

wip_qty = wr_qty_outque + wr_qty_rejque.

if qty_chg > 0 then do:
   if wip_qty <> 0 then do:
      assign
         unit_lbr_ll = (wr_lbr_ll_act - wr_lbr_ll_totx) / wip_qty
         unit_bdn_ll = (wr_bdn_ll_act - wr_bdn_ll_totx) / wip_qty
         unit_sub_ll = (wr_sub_ll_act - wr_sub_ll_totx) / wip_qty
         unit_mtl_ll = (wr_mtl_ll_act - wr_mtl_ll_totx) / wip_qty

         unit_lbr_tl = (wr_lbr_act - wr_lbr_totx) / wip_qty
         unit_bdn_tl = (wr_bdn_act - wr_bdn_totx) / wip_qty
         unit_sub_tl = (wr_sub_act - wr_sub_totx) / wip_qty
         unit_mtl_tl = (wr_mtl_act - wr_mtl_totx) / wip_qty

         lbr_ll_wipval_xfer = unit_lbr_ll * qty_chg
         bdn_ll_wipval_xfer = unit_bdn_ll * qty_chg
         sub_ll_wipval_xfer = unit_sub_ll * qty_chg
         mtl_ll_wipval_xfer = unit_mtl_ll * qty_chg

         lbr_tl_wipval_xfer = unit_lbr_tl * qty_chg
         bdn_tl_wipval_xfer = unit_bdn_tl * qty_chg
         sub_tl_wipval_xfer = unit_sub_tl * qty_chg
         mtl_tl_wipval_xfer = unit_mtl_tl * qty_chg.

      /* THE FOLLOWING SECTION ENSURES THAT THE _wipval_xfer
      FIELDS WON'T CAUSE THEIR RESPECTIVE _totx FIELDS TO EXCEED
      THEIR RESPECTIVE _act FIELDS.  THEN THE UNIT COSTS ARE
      RECALCULATED JUST IN CASE THE _wipval_xfer FIELDS CHANGE */

      {gprun.i ""remaxfer.p""
         "(input wr_lbr_ll_act, input wr_lbr_ll_totx,
           input-output lbr_ll_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_bdn_ll_act, input wr_bdn_ll_totx,
           input-output bdn_ll_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_sub_ll_act, input wr_sub_ll_totx,
           input-output sub_ll_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_mtl_ll_act, input wr_mtl_ll_totx,
           input-output mtl_ll_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_lbr_act, input wr_lbr_totx,
           input-output lbr_tl_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_bdn_act, input wr_bdn_totx,
           input-output bdn_tl_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_sub_act, input wr_sub_totx,
           input-output sub_tl_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_mtl_act, input wr_mtl_totx,
           input-output mtl_tl_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


   end. /* IF WIP_QTY <> 0 */
   else do:
      assign
         lbr_ll_wipval_xfer = wr_lbr_ll_act - wr_lbr_ll_totx
         bdn_ll_wipval_xfer = wr_bdn_ll_act - wr_bdn_ll_totx
         sub_ll_wipval_xfer = wr_sub_ll_act - wr_sub_ll_totx
         mtl_ll_wipval_xfer = wr_mtl_ll_act - wr_mtl_ll_totx

         lbr_tl_wipval_xfer = wr_lbr_act - wr_lbr_totx
         bdn_tl_wipval_xfer = wr_bdn_act - wr_bdn_totx
         sub_tl_wipval_xfer = wr_sub_act - wr_sub_totx
         mtl_tl_wipval_xfer = wr_mtl_act - wr_mtl_totx

         unit_lbr_ll = lbr_ll_wipval_xfer / qty_chg
         unit_bdn_ll = bdn_ll_wipval_xfer / qty_chg
         unit_sub_ll = sub_ll_wipval_xfer / qty_chg
         unit_mtl_ll = mtl_ll_wipval_xfer / qty_chg

         unit_lbr_tl = lbr_tl_wipval_xfer / qty_chg
         unit_bdn_tl = bdn_tl_wipval_xfer / qty_chg
         unit_sub_tl = sub_tl_wipval_xfer / qty_chg
         unit_mtl_tl = mtl_tl_wipval_xfer / qty_chg.
   end. /* IF WIP_QTY = 0 */
end. /* If QTY_CHG > 0 */
else do:

   /*       IN CASE OF NEGATIVE RECEIPTS DETERMINE THE UNIT COST OF THE    */
   /*       PREVIOUS RECEIPT BY DIVIDING THE PREVIOUSLY POSTED TOTAL COSTS */
   /*       (_totx) BY THE ACCUMULATED QUANTITY PREVIOUSLY RECEIVED        */
   /*       (wr_qty_cummove at the last operation). THE PREVIOUS UNIT COST */
   /*       IS MULTIPLIED BY THE QUANTITY REPORTED IN THIS TRANSACTION TO  */
   /*       DETERMINE THE RESULTING INCREMENTAL CHANGE TO THE _totx fields.*/

   if wr_qty_cummove <> 0 then do:
      assign
         unit_lbr_ll = wr_lbr_ll_totx / wr_qty_cummove
         unit_bdn_ll = wr_bdn_ll_totx / wr_qty_cummove
         unit_sub_ll = wr_sub_ll_totx / wr_qty_cummove
         unit_mtl_ll = wr_mtl_ll_totx / wr_qty_cummove

         unit_lbr_tl = wr_lbr_totx / wr_qty_cummove
         unit_bdn_tl = wr_bdn_totx / wr_qty_cummove
         unit_sub_tl = wr_sub_totx / wr_qty_cummove
         unit_mtl_tl = wr_mtl_totx / wr_qty_cummove

         lbr_ll_wipval_xfer = unit_lbr_ll * qty_chg
         bdn_ll_wipval_xfer = unit_bdn_ll * qty_chg
         sub_ll_wipval_xfer = unit_sub_ll * qty_chg
         mtl_ll_wipval_xfer = unit_mtl_ll * qty_chg

         lbr_tl_wipval_xfer = unit_lbr_tl * qty_chg
         bdn_tl_wipval_xfer = unit_bdn_tl * qty_chg
         sub_tl_wipval_xfer = unit_sub_tl * qty_chg
         mtl_tl_wipval_xfer = unit_mtl_tl * qty_chg.
   end. /* IF WR_QTY_CUMMOVE <> 0*/

   else do:

      /* REPORTING NEGATIVE PRODUCTION TAKES INVENTORY INTO WIP */
      /* THE REPORTED NEGATIVE PRODUCTION SHOULD BE TAKEN BACK  */
      /* INTO WIP AT THE LATEST GL COST                         */

      assign
         unit_lbr_ll = 0
         unit_bdn_ll = 0
         unit_sub_ll = 0
         unit_mtl_ll = sct_mtl_tl + sct_mtl_ll

         unit_lbr_tl = sct_lbr_tl + sct_lbr_ll
         unit_bdn_tl = sct_bdn_tl + sct_bdn_ll
         unit_sub_tl = sct_sub_tl + sct_sub_ll
         unit_mtl_tl = 0

         lbr_ll_wipval_xfer = unit_lbr_ll * qty_chg
         bdn_ll_wipval_xfer = unit_bdn_ll * qty_chg
         sub_ll_wipval_xfer = unit_sub_ll * qty_chg
         mtl_ll_wipval_xfer = unit_mtl_ll * qty_chg

         lbr_tl_wipval_xfer = unit_lbr_tl * qty_chg
         bdn_tl_wipval_xfer = unit_bdn_tl * qty_chg
         sub_tl_wipval_xfer = unit_sub_tl * qty_chg
         mtl_tl_wipval_xfer = unit_mtl_tl * qty_chg.
   end. /* IF WR_QTY_CUMMOVE = 0 */
end. /* IF QTY_CHG < 0 */

assign
   wr_lbr_ll_totx = wr_lbr_ll_totx + lbr_ll_wipval_xfer
   wr_bdn_ll_totx = wr_bdn_ll_totx + bdn_ll_wipval_xfer
   wr_sub_ll_totx = wr_sub_ll_totx + sub_ll_wipval_xfer
   wr_mtl_ll_totx = wr_mtl_ll_totx + mtl_ll_wipval_xfer

   wr_lbr_totx = wr_lbr_totx + lbr_tl_wipval_xfer
   wr_bdn_totx = wr_bdn_totx + bdn_tl_wipval_xfer
   wr_sub_totx = wr_sub_totx + sub_tl_wipval_xfer
   wr_mtl_totx = wr_mtl_totx + mtl_tl_wipval_xfer.

/*UPDATE COSTS*/

if qty_chg <> 0 then do:

   if cur_mthd = "avg" then do:

      assign
         unit_ovh_ll = 0
         unit_ovh_tl = 0.

      /*DO NOT INCLUDE OVERHEAD IN AVERAGING */
      for each spt_det
            fields (spt_cst_tl spt_element spt_part spt_pct_ll
            spt_sim spt_site)
            where spt_site = sct_site and
            spt_sim  = sct_sim  and
            spt_part = sct_part and
            truncate(spt_pct_ll,0) = 4 no-lock:
         unit_ovh_tl = unit_ovh_tl + spt_cst_tl.
      end.

      assign
         newcst = unit_mtl_tl + unit_lbr_tl + unit_bdn_tl + unit_ovh_tl
         + unit_sub_tl + unit_mtl_ll + unit_lbr_ll + unit_bdn_ll
         + unit_ovh_ll + unit_sub_ll
         reavg_yn = yes.

      if  (sct_mtl_tl = 0) and (sct_mtl_ll = unit_mtl_tl + unit_mtl_ll)
         and (sct_lbr_tl = unit_lbr_tl + unit_lbr_ll)
         and (sct_bdn_tl = unit_bdn_tl + unit_bdn_ll)
         and (sct_ovh_tl = unit_ovh_tl)
         and (sct_sub_tl = unit_sub_tl + unit_sub_ll)
         and (sct_cst_tot = newcst)

      then do:
         reavg_yn = no.
      end.
      else do:
         assign
            curflag = yes
            reavg_yn = yes.

         /*DETERMINE IF COST SHOULD BE AVERAGED OR REPLACED*/
         if (qty_oh + qty_chg <= 0)
            or (((qty_oh * sct_cst_tot) + (qty_chg * newcst))
            / (qty_oh + qty_chg) <= 0)
            or (qty_oh) <= 0
            then use_last = yes.

      end. /* Need to Reaverage */

      if reavg_yn and not use_last then do:

         /*UPDATE AVERAGE CURRENT COST*/
         assign
            newmtl_ll = (sct_mtl_ll * qty_oh + mtl_tl_wipval_xfer
            + mtl_ll_wipval_xfer) / new_qty_oh

            newmtl_tl = (sct_mtl_tl * qty_oh) / new_qty_oh
            newlbr_tl = ((sct_lbr_tl + sct_lbr_ll) * qty_oh
            + lbr_tl_wipval_xfer + lbr_ll_wipval_xfer) / new_qty_oh
            newbdn_tl = ((sct_bdn_tl + sct_bdn_ll) * qty_oh
            + bdn_tl_wipval_xfer + bdn_ll_wipval_xfer) / new_qty_oh
            newsub_tl = ((sct_sub_tl + sct_sub_ll) * qty_oh
            + sub_tl_wipval_xfer + sub_ll_wipval_xfer) / new_qty_oh

            sct_mtl_ll = newmtl_ll
            sct_lbr_ll = 0
            sct_bdn_ll = 0
            sct_sub_ll = 0
            sct_ovh_ll = 0
            sct_mtl_tl = newmtl_tl
            sct_lbr_tl = newlbr_tl
            sct_bdn_tl = newbdn_tl
            sct_sub_tl = newsub_tl
            /*       WE DO NOT WANT TO CHANGE THE THIS-LEVEL OVERHEAD COST */
            sct_ovh_tl = sct_ovh_tl

            sct_cst_tot =
            sct_mtl_tl + sct_lbr_tl + sct_bdn_tl + sct_ovh_tl +
            sct_sub_tl + sct_mtl_ll + sct_lbr_ll + sct_bdn_ll +
            sct_ovh_ll + sct_sub_ll
            sct_cst_date = today.

         /*UPDATE SPT_DET*/
         {gprun.i ""reupdspt.p"" "(input recid(sct_det))"}
/*GUI*/ if global-beam-me-up then undo, leave.

      end. /* reavg_yn & not use_last */
   end. /* cur_mthd = "avg" */

   if cur_mthd = "last" or use_last then do:
      curflag = yes.

      /*       UPDATE LAST CURRENT COST */
      assign
         sct_mtl_tl = 0
         sct_lbr_tl = max(0,unit_lbr_tl + unit_lbr_ll)
         sct_bdn_tl = max(0,unit_bdn_tl + unit_bdn_ll)
         sct_sub_tl = max(0,unit_sub_tl + unit_sub_ll)
         /*        WE DO NOT WANT TO CHANGE THE THIS-LEVEL OVERHEAD COST */
         sct_ovh_tl = sct_ovh_tl
         sct_mtl_ll = max(0,unit_mtl_tl + unit_mtl_ll)
         sct_lbr_ll = 0
         sct_bdn_ll = 0
         sct_sub_ll = 0
         sct_ovh_ll = 0.

      sct_cst_tot =
      sct_mtl_tl + sct_lbr_tl + sct_bdn_tl + sct_ovh_tl +
      sct_sub_tl + sct_mtl_ll + sct_lbr_ll + sct_bdn_ll +
      sct_ovh_ll + sct_sub_ll.
      sct_cst_date = today.

      /*UPDATE SPT_DET*/
      {gprun.i ""reupdspt.p"" "(input recid(sct_det))"}
/*GUI*/ if global-beam-me-up then undo, leave.

   end. /* cur_mthd = last */

   /*          MATCHES THE COST SIMULATION ITEM DETAIL                     */
   {gprun.i ""csavg2b.p"" "(input wo_part,
        input wo_site,
        input cur_set,
        input curflag,
        input glx_set,
        input glxflag)"
      }
/*GUI*/ if global-beam-me-up then undo, leave.


end. /* qty_chg > 0 */
